package org.lucava.boatskeeper.client.presenter;

import java.util.ArrayList;
import java.util.List;

import org.lucava.boatskeeper.client.BoatsKeeperServiceAsync;
import org.lucava.boatskeeper.client.event.boat.AddBoatEvent;
import org.lucava.boatskeeper.client.event.boat.EditBoatEvent;
import org.lucava.boatskeeper.shared.Boat;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class BoatsPresenter implements Presenter {

	private List<Boat> boats = new ArrayList<Boat>();

	public interface Display {
		HasClickHandlers getAddButton();

		HasClickHandlers getDeleteButton();

		HasClickHandlers getList();

		void setData(List<String> data);

		int getClickedRow(ClickEvent event);

		List<Integer> getSelectedRows();

		Widget asWidget();
	}

	private final BoatsKeeperServiceAsync rpcService;
	private final HandlerManager eventBus;
	private final Display display;

	public BoatsPresenter(BoatsKeeperServiceAsync rpcService,
			HandlerManager eventBus, Display view) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
	}

	public void bind() {
		display.getAddButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new AddBoatEvent());
			}
		});

		display.getDeleteButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				deleteSelectedBoats();
			}
		});

		display.getList().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int selectedRow = display.getClickedRow(event);

				if (selectedRow >= 0) {
					Integer id = boats.get(selectedRow).getId();
					eventBus.fireEvent(new EditBoatEvent(id));
				}
			}
		});
	}

	public void go(final HasWidgets container) {
		bind();
		container.clear();
		container.add(display.asWidget());
		fetchBoats();
	}

	private void fetchBoats() {
		rpcService.getBoats(new AsyncCallback<List<Boat>>() {
			public void onSuccess(List<Boat> result) {
				boats = result;
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(boats.get(i).getName());
				}

				display.setData(data);
			}

			public void onFailure(Throwable caught) {
				Window.alert("Error fetching boat details");
			}
		});
	}

	private void deleteSelectedBoats() {
		List<Integer> selectedRows = display.getSelectedRows();
		ArrayList<Integer> ids = new ArrayList<Integer>();

		for (int i = 0; i < selectedRows.size(); ++i) {
			ids.add(boats.get(selectedRows.get(i)).getId());
		}

		rpcService.deleteBoats(ids, new AsyncCallback<List<Boat>>() {
			public void onSuccess(List<Boat> result) {
				boats = result;
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(boats.get(i).getName());
				}

				display.setData(data);

			}

			public void onFailure(Throwable caught) {
				Window.alert("Error deleting selected boats");
			}
		});
	}
}
